package com.xmall.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.base.ResultCode;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.validate.group.AddGroup;
import com.xmall.validate.group.UpdateGroup;
import com.xmall.sys.define.dto.role.SysRoleDto;
import com.xmall.sys.define.request.role.RoleListPageQueryRequest;
import com.xmall.sys.define.request.role.RoleQueryRequest;
import com.xmall.sys.define.request.role.RoleUpdateRequest;
import com.xmall.sys.define.request.role.RolePermissionUpdateRequest;
import com.xmall.sys.service.IRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统角色
 */
@RequestMapping("/service/sys/role")
@RestController
public class RoleController {

    @Resource
    private IRoleService roleService;

    /**
     * 角色关联的权限ID列表
     */
    @GetMapping("/permissionIds/{roleId}")
    public Result<List<Long>> rolePermissionIds(@PathVariable("roleId") Long roleId){
        List<Long> rolePermissionIds = roleService.rolePermissionIds(roleId);
        return Result.success(rolePermissionIds);
    }

    /**
     * 更新角色关联的权限列表
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:role:permission')")
    @PostMapping("/updateRolePermission")
    public Result updateRolePermission(@Validated @RequestBody RolePermissionUpdateRequest request){
        roleService.updateRolePermission(request);
        return Result.success();
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public Result<SysRoleDto> info(@PathVariable("id") Long id){
        SysRoleDto sysRoleDto = roleService.info(id);
        return null != sysRoleDto ? Result.success(sysRoleDto) : Result.error(ResultCode.NOT_EXIST);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result<List<SysRoleDto>> list(RoleQueryRequest query){
        List<SysRoleDto> list = roleService.list(query);
        return Result.success(list);
    }

    /**
     * 分页列表
     */
    @PreAuthorize("hasAuthority('sys:role:list')")
    @GetMapping("/listPage")
    public Result<Page<SysRoleDto>> listPage(@Validated RoleListPageQueryRequest query){
        Page<SysRoleDto> pageList = roleService.listPage(query);
        return Result.success(pageList);
    }

    /**
     * 保存
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PreAuthorize("hasAuthority('sys:role:save')")
    @PostMapping("/save")
    public Result saveEntity(@Validated(value = AddGroup.class) @RequestBody RoleUpdateRequest request){
        roleService.saveEntity(request);
        return Result.success();
    }

    /**
     * 编辑
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:role:update')")
    @PostMapping("/update")
    public Result updateEntity(@Validated(value = UpdateGroup.class) @RequestBody RoleUpdateRequest request){
        roleService.updateEntity(request);
        return Result.success();
    }

    /**
     * 删除
     */
    @OperateRecord(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('sys:role:delete')")
    @PostMapping("/delete/{id}")
    Result delete(@PathVariable("id") Long id){
        roleService.deleteEntity(id);
        return Result.success();
    }
}
